<template>
  <div>
    <!-- 渲染角色列表 -->
    <!-- 复选框 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
      <!-- <el-checkbox label="复选框 B" /> -->
      <!-- <el-checkbox label="复选框 C" /> -->
    </el-checkbox-group>

    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="setRoles">确定</el-button>
        <el-button size="small" @click="cancleDialog">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
// 获取所有角色列表api
import { getPolesAPI } from '@/api/setting'
// 获取
import { getUserDetailIdAPI } from '@/api/user'
// 给员工分配角色
import { assignRolesAPI } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    // 用户的id 用来查询当前用户的角色信息
    roleId: {
      type: [String, Number],
      default: null
    }
  },
  data() {
    return {
      roleIds: [], // 绑定 收集点击的复选项
      roleList: []
    }
  },
  created() {
    // 调用获取所有角色的方法
    this.getPolesFn()
    // 调用获取已有角色的方法
    this.getUserDetailIdFn()
  },
  methods: {

    // 分配角色
    async setRoles() {
      const res = await assignRolesAPI({ id: this.roleId, roleIds: this.roleIds })
      console.log(res)
      this.$message.success(res.message)
      this.$emit('close')
    },

    // 获取已有角色
    async getUserDetailIdFn() {
      const res = await getUserDetailIdAPI(this.roleId)
      console.log(res)
    //   this.roleIds = res.data.roleIds === null ? [] : res.data.roleIds
    },
    // 获取所有角色列表
    async getPolesFn() {
      const res = await getPolesAPI()
      console.log(res)
      this.roleList = res.data.rows
    },
    // 取消弹框
    cancleDialog() {
    //   this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>
.footer {
  margin-top: 30px;
}
</style>
